/**
 * 
 */
package wong.board.client.ui;

import java.util.ArrayList;
import java.util.Set;

import wong.board.client.util.JSONTool;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;

/**
 * @author wong
 * 
 */
public class ShowcasesPresenter extends Presenter<ShowcasesPresenter.View, JSONArray> {

	private JSONArray showcases;

	public ShowcasesPresenter() {
		this((View) GWT.create(View.class));
	}

	public ShowcasesPresenter(View view) {
		super(view);
	}

	@Override
	public void updateView(JSONArray newShowcases) {
		if (newShowcases != null) {
			if (showcases == null) {
				showcases = newShowcases;
			} else {
				showcases = JSONTool.merge(showcases, newShowcases, true);
			}
			//
			View.Data data = new View.Data();
			int size = showcases.size();
			for (int i = 0; i < size; i++) {
				JSONArray showcase = showcases.get(i).isArray();
				String columnTitle = showcase.get(0).isString().stringValue();
				String dataLabel = showcase.get(1).isString().stringValue();
				String tooltip = showcase.get(2).isString().stringValue();
				JSONObject dataObject = showcase.get(3).isObject();
				Set<String> keySet = dataObject.keySet();
				for (String rowTitle : keySet) {
					String link = dataObject.get(rowTitle).isString().stringValue();
					if (link.length() > 0) {
						String linkTooltip = columnTitle + ": " + dataLabel + " - " + tooltip + " (" + link
								+ ")";
						data.add(rowTitle, columnTitle, dataLabel, link, linkTooltip);
					}
				}
			}

			getView().setData(data);
		}
	}

	public interface View extends Presenter.View {

		void setData(Data data);

		public static class Data {

			public ArrayList<String> rows = new ArrayList<String>();
			public ArrayList<String> columns = new ArrayList<String>();
			private ArrayList<ArrayList<ArrayList<LinkInfo>>> table = new ArrayList<ArrayList<ArrayList<LinkInfo>>>();

			public void add(String rowTitle, String columnTitle, String label, String link, String tooltip) {
				int rowIndex = getRowIndex(rowTitle, true);
				int columnIndex = getColumnIndex(columnTitle, true);
				ArrayList<LinkInfo> cell = getCell(rowIndex, columnIndex, true);
				if (cell != null) {
					cell.add(new LinkInfo(label, link, tooltip));
				}
			}

			protected int getRowIndex(String rowTitle, boolean create) {
				int index = rows.indexOf(rowTitle);
				if (index < 0 && create) {
					index = rows.size();
					rows.add(rowTitle);
				}
				return index;
			}

			protected int getColumnIndex(String columnTitle, boolean create) {
				int index = columns.indexOf(columnTitle);
				if (index < 0 && create) {
					index = columns.size();
					columns.add(columnTitle);
				}
				return index;
			}

			protected ArrayList<ArrayList<LinkInfo>> getRow(int rowIndex, boolean create) {
				ArrayList<ArrayList<LinkInfo>> row = rowIndex >= table.size() ? null : table.get(rowIndex);
				if (row == null && create) {
					while (table.size() < rowIndex + 1) {
						ArrayList<ArrayList<LinkInfo>> newRow = new ArrayList<ArrayList<LinkInfo>>();
						table.add(newRow);
					}
					row = new ArrayList<ArrayList<LinkInfo>>();
					table.set(rowIndex, row);
				}
				return row;
			}

			protected ArrayList<ArrayList<LinkInfo>> getRow(int rowIndex) {
				return getRow(rowIndex, false);
			}

			protected ArrayList<LinkInfo> getCell(int rowIndex, int columnIndex, boolean create) {
				ArrayList<LinkInfo> cell = null;
				ArrayList<ArrayList<LinkInfo>> row = getRow(rowIndex, create);
				if (row != null) {
					cell = columnIndex >= row.size() ? null : row.get(columnIndex);
					if (cell == null && create) {
						while (row.size() < columnIndex + 1) {
							ArrayList<LinkInfo> newCell = new ArrayList<LinkInfo>();
							row.add(newCell);
						}
						cell = new ArrayList<ShowcasesPresenter.View.Data.LinkInfo>();
						row.set(columnIndex, cell);
					}
				}
				return cell;
			}

			public ArrayList<LinkInfo> getCell(int rowIndex, int columnIndex) {
				return getCell(rowIndex, columnIndex, false);
			}

			public static class LinkInfo {
				String label;
				String link;
				String tooltip;

				public LinkInfo(String label, String link, String tooltip) {
					this.label = label;
					this.link = link;
					this.tooltip = tooltip;
				}
			}

		}

	}

}
